package com.igeek.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.igeek.domain.Category;
import com.igeek.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
public class OtherInterceptor implements HandlerInterceptor {
	@Autowired
    CategoryService categoryService;
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        return true;   
    }
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {


        List<Category> cs = (List<Category>) httpServletRequest.getServletContext().getAttribute("categories_below_search");
        if(cs == null){
             cs =categoryService.list();
            httpServletRequest.getServletContext().setAttribute("categories_below_search", cs);
            log.info("categories_below_search:"+"categories_below_search");
        }
    	String contextPath=httpServletRequest.getServletContext().getContextPath();
    	httpServletRequest.getServletContext().setAttribute("contextPath", contextPath);
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}

